package top.hkyzf.event;

import com.mojang.logging.LogUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.slf4j.Logger;
import top.hkyzf.util.ClientConfig;
import top.hkyzf.util.Utils;

import java.util.UUID;

/**
 * description: 实体相关事件
 * @author zhufeng
 * @date 2022/8/11 18:05
 * @version V1.0
 */
@Mod.EventBusSubscriber(value = Dist.CLIENT)
public class LivingEvents {
    /**
     * 获取一个 slf4j 日志记录器
     */
    private static final Logger LOGGER = LogUtils.getLogger();
    /**
     * 传送最低血量
     */
    private static final Float MIN_HEALTH = 3.0F;

    /**
     * 玩家受到伤害血量过低直接传送
     * LivingHurtEvent 需要在服务端才可监听到所有实体受伤事件，故客户端无法使用
     * @param event 实体受到伤害的事件
     */
    @SubscribeEvent
    public static void onLivingHurt(LivingHurtEvent event) {
        LocalPlayer player = Minecraft.getInstance().player;
        if (player != null) {
            UUID playerId = player.getUUID();
            UUID eventId = event.getEntityLiving().getUUID();
            LOGGER.info("玩家的UUID为：{}", playerId);
            LOGGER.info("玩家的血量为：{}", player.getHealth());
            LOGGER.info("受伤实体的UUID为：{}", eventId);
            // 自己受到伤害并且血量过低直接传送至安全区
            if (playerId.equals(eventId) && player.getHealth() < MIN_HEALTH) {
                Utils.sendCommand("/home " + ClientConfig.SAFE_HOME.get());
            }
        }
    }
}
